/**
 * Created by shidaiapp on 2017/9/29.
 */

function resetForm(self, tiyData) {
  self.$refs['modifyActionItem'].resetFields();
}

function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalModify.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalModify.isLoading = true;
  });

  tiyData.tStatus.modalDelete.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalDelete.isLoading = true;
  });

}

/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  //resetForm(self, tiyData);
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 20;
  tiyData.tPage.currentPage = 1;
  tiyData.tParams.actionModuleIdSearch = '';
  tiyData.tParams.actionNameSearch = '';
 };

/**
 * 添加新店铺
 * @param self
 * @param tiyData
 */
export const add = (self, tiyData) => {
  resetForm(self, tiyData);
  tiyData.tParams.action.id = 0;
  tiyData.tParams.action.moduleId = 0;
  tiyData.tParams.action.moduleName = "";
  tiyData.tParams.action.name = "";
  tiyData.tParams.action.sequence = 99;
  tiyData.tParams.action.note = "";
  tiyData.tParams.action.hiddenId = "";
  tiyData.tParams.action.operationJson = [{
    name: '',
    note: '',
    url: '',
    isMain: false
  }];
  tiyData.tStatus.modalModify.isModify = false;
  tiyData.tStatus.modalModify.isShow = true;
  tiyData.tStatus.modalModify.title = "新建功能";

};
/**
 * 删除
 * @param self
 * @param tiyData
 */
export const deleted = (self, tiyData, index) => {
  tiyData.tStatus.modalDelete.isShow = true;
  tiyData.tStatus.modalDelete.role = tiyData.tData.roleList[index];
};

/**
 * 编辑店铺
 * @param self
 * @param tiyData
 * @param index
 */
export const modify = (self, tiyData, index) => {
  resetForm(self, tiyData);
  self.$Spin.show();
  self.$apiRole.getFunctionById({
    id: tiyData.tData.actionList[index].id
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      var action = response.result;
      tiyData.tParams.action.id = action.id;
      tiyData.tParams.action.moduleId = action.moduleId;
      tiyData.tParams.action.moduleName = action.moduleName;
      tiyData.tParams.action.name = action.name;
      tiyData.tParams.action.sequence = action.sequence;
      tiyData.tParams.action.hiddenId = action.moduleId;
      tiyData.tParams.action.note = action.note;
      tiyData.tParams.action.operationJson.length = 0;
      action.operations.forEach(
        function (item) {
          tiyData.tParams.action.operationJson.push(
            {
              name: item.name,
              note: item.note,
              url: item.url,
              isMain: item.isMain
            });
        }
      );
      tiyData.tStatus.modalModify.isShow = true;
      tiyData.tStatus.modalModify.isModify = true;
    } else {
      self.$Message.error(response.errorMessage);
    }
  });

};

/**
 * 添加/修改数据 到服务器
 * @param self
 * @param tiyData
 * @param name
 */
export const webAddOrModify = function (self, tiyData, name) {
  tiyData.tStatus.modalModify.isLoading = true;
  self.$refs[name].validate((valid) => {
    if (valid) {
      var modifyActionItem = tiyData.tParams.action;
      let operationJson = [];
      modifyActionItem.operationJson.forEach(function (item) {
        if (('' != item.name || '' != item.note) && '' != item.url) {
          operationJson.push({name: item.name, note: item.note, url: item.url, isMain: false});
        }
      });
      if (0 == operationJson.length) {
        changeModelLoading(self, tiyData);
        self.$Message.error('必须有一条可操作的url');
        return;
      }
      var parms = {
        id: modifyActionItem.id,
        name: modifyActionItem.name,
        note: modifyActionItem.note,
        moduleId: modifyActionItem.moduleId,
        hiddenId: modifyActionItem.hiddenId,
        sequence: modifyActionItem.sequence,
        operationJson:  JSON.stringify(operationJson)
      };
      self.$apiRole.addFunction(parms)
        .then(function (response) {
          tiyData.tStatus.modalModify.isShow = false;
          changeModelLoading(self, tiyData);
          if (response.success) {
            webList(self, tiyData);
            self.$Message.success('提交成功!');
          } else {
            self.$Message.error(response.errorMessage);
          }
        });

    } else {
      changeModelLoading(self, tiyData);
    }
  });
};

/**
 * 从服务器删除
 * @param self
 * @param tiyData
 */
export const webDelete = function (self, tiyData) {
  tiyData.tStatus.modalDelete.isLoading = true;
  var parms = {
    id: tiyData.tStatus.modalDelete.action.id,
  };
  self.$apiRole.deleteFunction(parms)
    .then(function (response) {
      tiyData.tStatus.modalDelete.isShow = false;
      changeModelLoading(self, tiyData);
      if (response.success) {
        webList(self, tiyData);
        self.$Message.success('删除成功!');
      } else {
        tiyData.tStatus.modalDelete.isLoading = false;
        self.$Message.error(response.errorMessage);
      }
    });
};

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$apiRole.getFunctions({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "name": tiyData.tParams.actionNameSearch,
    "moduleId": tiyData.tParams.actionModuleIdSearch
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.actionList.length = 0;
      tiyData.tData.moduleList.length = 0;
      let actionPage = response.result.authRules;
      tiyData.tPage.total = actionPage.total;
      tiyData.tPage.stepNumber = actionPage.pageSize;
      actionPage.list.forEach(
        function (item) {
          tiyData.tData.actionList.push(item);
        }
      );
      // response.result.modules.forEach(
      //   function (item) {
      //     tiyData.tData.moduleList.push(item);
      //   }
      // );
      //
      self._.forEach(response.result.modules,function (item) {
        tiyData.tData.moduleList.push(item);
      });


      if(0 == tiyData.tData.actionList.length){
        self.$Message.error("查询没有结果");
      }
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};


